rm(list=ls())
gc()

library(maps)
library(ggplot2)
library(foreign)
library(plyr)
library(reshape2)
library(mapproj)

### PLOTS ###
theme_clean <- function (base_size = 12) { 
  require(grid) # Needed for unit() function
  theme_grey(base_size) %+replace%
    theme(
      axis.title = element_blank(),
      axis.text = element_blank(), panel.background = element_blank(),
      panel.grid = element_blank(), axis.ticks.length = unit(0, "cm"),
        axis.ticks.margin = unit(0, "cm"),
      panel.margin = unit(0, "lines"),
        plot.margin = unit(c(0, 0, 0, 0), "lines"), complete = TRUE
    ) 
}

POtoCode <- function (pos, dta=st.info) {
  return(as.integer(dta$ICPSRCode[match(pos, dta$POAbrv)]))
}
CodeToName <- function (stcodes, dta=st.info) {
  return(as.character(dta$Name[match(stcodes, dta$ICPSRCode)]))
}
FileName <- function (path=NULL, name=NULL, ext=NULL) {
  p <- paste(path, collapse = "")
  n <- paste(name, collapse = "")
  e <- paste(".", ext, sep = "")
  d <- format(Sys.Date(), "%y%m%d") 
  file_name <- paste(p, d, n, "-a", e, sep = "")  
  if (any(grepl(file_name, list.files()))) {
    for (ll in letters[-1]) {
      file_name <- paste(p, d, paste(n, ll, sep="-"), e, sep = "") 
      if (!any(grepl(file_name, list.files()))) break
    }
  }
  cat("\nFile name:", file_name, "\n\n")
  return(file_name)
}

liberalism.map<-read.csv(file="Fig-maps_data.csv")
pdf("Fig-figure1.pdf", height=3.5, width=5)
(p1 <- ggplot(data=liberalism.map,
        aes(x=long, y=lat, group=group, fill=liberalism0))
 + geom_polygon(colour="black", size=rel(.1))
 + coord_map("polyconic")
 + scale_fill_gradient2(name="Liberalism",
                       low='white', high='grey1', midpoint=-1)
   + facet_wrap(~Year, ncol=2)
 + theme_clean()+guides(fill=FALSE)
)
dev.off()
